/* eslint-disable no-case-declarations */
/* eslint-disable max-classes-per-file */
/* eslint-disable no-shadow */
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2024-05-03 10:33:51
 * @LastEditors: swc shanwenchao12138@163.com
 * @LastEditTime: 2025-03-11 15:35:19
 */

import { Tsrv } from '@/services';
import { T_eventHandle } from '../pubApi/eventHandle';

// 组件配置命令
enum V_compCfgCmd {
  DETAIL, // 直接写数据
  PUB_API, // 数据来源与pub_api请求
  COMP_CONF_CENTER // 数据来自组件配置中心
}
type pubApiName = string;
type compConfName = string;
export class T_btn {
  private type: string = 'button';

  private title: string;

  private disabled: boolean;

  private sort: number;

  private style: {
    color:
      | 'primary'
      | 'success'
      | 'warning'
      | 'info'
      | 'error'
      | 'ghost'
      | 'dashed';
    size: 'large' | 'small' | 'default';
    shape: 'circle';
    icon: string;
  };

  constructor() {
    this.disabled = false;
    this.style = {
      color: 'primary',
      size: 'default',
      shape: null,
      icon: null
    };
  }

  event: { click: pubApiName } | { dbClick: pubApiName };
}
export interface I_btn extends T_btn {}

export class T_progressBar {
  type: string;
  // private content: {
  //   size: 'large' | 'small' | 'default';
  //   showInfo: boolean;
  //   value: number;
  //   nodeStyle: {};
  // };
}
export interface I_progressBar extends T_progressBar {}

export type I_compCfgDetailComps = I_btn | I_progressBar;
export class T_compCfgDetail {
  comps: Array<I_compCfgDetailComps>;
}
export interface I_compCfgDetail extends T_compCfgDetail {}

export interface I_compConfCenter {
  controlTitle: compConfName;
}

export interface I_pubApi {
  pubApiName: pubApiName;
}

export type I_compCfgData = I_compCfgDetail | I_compConfCenter | I_pubApi;
export class T_compCfg extends T_eventHandle {
  private detailComps = [];

  // 命令
  private cmd: V_compCfgCmd;

  private data: I_compCfgData;

  private loaded: (d: Array<I_compCfgDetailComps>) => void;

  constructor(callback: (d: Array<I_compCfgDetailComps>) => void) {
    super();
    this.data = null;
    this.cmd = null;
    this.loaded = callback;
  }

  public currDataSource(): V_compCfgCmd {
    return this.cmd;
  }

  public setCmd(p: V_compCfgCmd): T_compCfg {
    this.cmd = p;
    return this;
  }

  public loadData() {
    if (this.data !== null) {
      if (this.cmd === null) {
        console.error('控制字为空');
        return;
      }

      switch (this.cmd) {
        case V_compCfgCmd.DETAIL:
          this.getDataByCfgDet();
          break;
        case V_compCfgCmd.PUB_API:
          this.getDataByPubApi();
          break;
        case V_compCfgCmd.COMP_CONF_CENTER:
          this.getDataByCompConfCenter();
          break;
        default:
          console.error('未定义的类型');
      }
    }
  }

  public setCfgData(p: I_compCfgData): T_compCfg {
    this.data = p;
    return this;
  }

  private getDataByCfgDet() {
    const d = this.data as I_compCfgDetail;
    this.detailComps = d.comps;
    this.loaded(this.detailComps);
  }

  private getDataByCompConfCenter() {
    const d = this.data as I_compConfCenter;
    this.getDataWithOption(
      this.getSysDefLang(),
      this.lexicon.globalVar.dbType.MYSQL,
      this.lexicon.globalVar.tableName.CONTROL_CFG,
      ['f_type', 'content'],
      { title: { $eq: d.controlTitle } }
    )
      .then(([ok, res, row, err]) => {
        if (!ok || !res.success) {
          console.error(err ?? res.msg);
          return;
        }
        if (res.data.total > 0) {
          res.data.rows.forEach(el => {
            this.detailComps.push(...JSON.parse(el.content).comps);
          });

          this.loaded(this.detailComps);
        }
      })
      .catch(er => console.error(er));
  }

  private getDataByPubApi() {
    const d = this.data as I_pubApi;
    this.getApiDet(d.pubApiName)
      .then(([ok, res, rows, err]) => {
        if (!ok || !res.success) {
          console.error(err ?? res.msg);
          return;
        }
        const copms = [];
        if (
          Tsrv.noValue(rows[0]['content']) &&
          Array.isArray(rows[0]['content'])
        ) {
          copms.push(...rows[0]['content']);
        }
        this.detailComps.push(...copms);
        this.loaded(this.detailComps);
      })
      .catch(er => console.error(er));
  }
}
export interface I_compCfg extends T_compCfg {}
